//获取管理员列表
var table = $("#staff_List").dataTable({
    //"iDisplayLength":10,
    "ajax": {
        "url":"/role/get-admin-list",
        "type":'post',
        "data": function (d) {
            //添加额外的参数传给服务器
            //return $.extend({ CurrentPage:0, PageSize: 10}, {});
        }},
    'bPaginate': true,
    "bDestory": true,
    "bRetrieve": true,
    "bFilter":false,
    "bSort": true,
    "bProcessing": true,
    "autoWidth": false,
    "searching":true,
    "aoColumns": [
        {"mDataProp": "AdminName"},
        {"mDataProp": "AdminNameLower"},
        {"mDataProp": "AdminStateName"},
        {"mDataProp": "RoleNames"},
        {
            "mDataProp": "AdminId",
            "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                $(nTd).html('<button type="button" class="btn btn-success btn-xs LockAdmin"  data-adminid="'+oData.AdminId+'" data-adminstatic="'+((oData.AdminState == 0) ? 10 : 0 )+'"><i class="fa fa-'+(oData.AdminState == 0 ? 'lock':'unlock')+'"></i>'+(oData.AdminState == 0 ? ' 锁定':'激活')+'</button>\
                <button type="button" class="btn btn-warning btn-xs ResetPassword"  data-adminid="'+oData.AdminId+'"><i class="fa fa-refresh"></i> 重置密码</button>')
                    .append(' <button type="button" class="btn btn-info btn-xs EditAdmin" data-adminname="'+oData.AdminName+'" data-adminnamelower="'+oData.AdminNameLower+'" data-adminid="'+oData.AdminId+'" ><i class="fa fa-pencil-square-o" ></i> 编辑</button>')
                    .append(' <button type="button" class="btn btn-danger btn-xs DelAdmin" data-adminid="'+oData.AdminId+'" ><i class="fa fa-times"></i> 删除</button>');

            }
        },
    ],
    "fnCreatedRow": function (nRow, aData, iDataIndex) {
        //add selected class
        $(nRow).click(function () {
            if ($(this).hasClass('row_selected')) {
                $(this).removeClass('row_selected');
            } else {
                table.$('tr.row_selected').removeClass('row_selected');
                $(this).addClass('row_selected');
            }
        });
    },
    "fnInitComplete": function (oSettings, json) {
    }
});

//获取角色列表
function getRoleList(){
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/get-role-list',
        success: function (ret) {
            var str = '';
            $(ret.Models).each(function(k,v){
                str += ' <option value="'+ v.RoleId+'">'+ v.RoleName+'</option>'
            });
            $("#RoleId").append(str);
        }
    });
}
getRoleList();

//添加管理员
function AddAdmin(){
    var JsonData = {
        AdminName : $("#AdminName").val(),
        AdminNameLower : $("#AdminNameLower").val(),
        Pwd : $("#UserPassword").val(),
        RoleId : $("#RoleId").val()
    }
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/admin-save',
        data:JsonData,
        success: function (ret) {
            $("#AddAdmin").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#staff_List').DataTable();
                table.ajax.reload();
                layer.msg('添加成功！');
            }else{
                layer.msg('添加失败！');
            }
        }
    });
}
//修改管理员
function EditAdmin(){
    var JsonData ={
        AdminId : $("#EditForm").attr('data-adminid'),
        AdminName :  $("#EditAdminName").val(),
        AdminNameLower : $("#EditAdminNameLower").val(),
    }
    $.ajax({
        type: "post",
        dataType: "json",
        url: '/role/admin-save',
        data:JsonData,
        success: function (ret) {
            $("#EditAdmin").modal('hide');
            resetFrom();
            if(ret.IsBizSuccess){
                var table = $('#staff_List').DataTable();
                table.ajax.reload();
                layer.msg('修改成功！');
            }else{
                layer.msg('修改失败！');
            }
        }
    });

}

//删除管理员
$(document).on('click','.DelAdmin',function(){
    var AdminId = $(this).data("adminid");
    layer.confirm('确定删除吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type: "post",
            dataType: "json",
            url: '/role/admin-del',
            data:{AdminId:[AdminId]},
            success: function (ret) {
                if(ret.IsBizSuccess){
                    var table = $('#staff_List').DataTable();
                    table.ajax.reload();
                    layer.msg('成功删除！');
                }else{
                    layer.msg('删除失败！');
                }
            }
        });
    }, function(){
    });
});

//锁定
$(document).on('click','.LockAdmin',function(){
    var AdminId = $(this).data("adminid");
    var AdminState = $(this).data("adminstatic");
    layer.confirm('确定此操作吗吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type: "post",
            dataType: "json",
            url: '/role/admin-set-state',
            data:{AdminId:AdminId,AdminState:AdminState},
            success: function (ret) {
                if(ret.IsBizSuccess){
                    var table = $('#staff_List').DataTable();
                    table.ajax.reload();
                    layer.msg('操作成功！');
                }else{
                    layer.msg('操作失败！');
                }
            }
        });
    }, function(){
    });
});
//重置密码
$(document).on('click','.ResetPassword',function(){
    var AdminId = $(this).data("adminid");
    layer.confirm('确定此操作吗吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            type: "post",
            dataType: "json",
            url: '/role/admin-reset-pwd',
            data:{AdminId:AdminId},
            success: function (ret) {
                if(ret.IsBizSuccess){
                    var table = $('#staff_List').DataTable();
                    table.ajax.reload();
                    layer.msg('操作成功！');
                }else{
                    layer.msg('操作失败！');
                }
            }
        });
    }, function(){
    });
});

//选中
$(document).on('click','.EditAdmin',function(){
    var AdminId = $(this).data("adminid");
    var AdminName = $(this).data("adminname");
    var AdminLower = $(this).data("adminnamelower");
    $("#EditAdminName").val(AdminName);
    $("#EditAdminNameLower").val(AdminLower);
    $("#EditForm").attr('data-adminid',AdminId);
    $("#EditAdmin").modal('show');
});

//隐藏模态窗口
$("#AddAdmin").on("hide.bs.modal", function () {
    setTimeout(function(){
        resetFrom();
    },100)

});

$("#AddForm").validate({
    submitHandler:function() {
        AddAdmin();
    },//这是关键的语句，配置这个参数后表单不会自动提交，验证通过之后会去调用的方法
    rules:{
        UserPassword:{
            minlength: 6,
            maxlength: 12
        },
        AgainPassword:{
            minlength: 6,
            maxlength: 12,
            equalTo: "#UserPassword"
        }
    },
    messages: {
        AgainPassword: {
            minlength: "密码长度需为6~12",
            maxlength: "密码长度需为6~12"
        },
        AgainPassword: {
            minlength: "密码长度需为6~12",
            maxlength: "密码长度需为6~12",
            equalTo: "两次输入密码不一致"
        }
    }
});

$("#EditForm").validate({
        submitHandler:function() {
            EditAdmin();
        }
});


//重置表单
function resetFrom() {
    $('form').each(function (index) {
        $('form')[index].reset();
    });
}